

const BaseController = require('./base_controller.js');
const contentCheck = require('../../../../framework/validate/content_check.js');
const cloudBase = require('../../../../framework/cloud/cloud_base.js');
const { LBS_KEY } = require('../../../../config/config.js');
const request = require('../../utils/request.js');

class CommonController extends BaseController {

    /**
     * 获取手机号
     */
    async getwxphone() {
        let cloudID = this._event.params['cloudID'] ?? '';
        let phoneNumber = '';
        // 手机注册
        let cloud = cloudBase.getCloud();
        const apiData = await cloud.getOpenData({
            "list": [cloudID]
        })
        if (apiData.list[0].data.phoneNumber) {
            phoneNumber = apiData.list[0].data.phoneNumber;
        }
        let res = {
            phone: phoneNumber
        };
        return res;
    }

    /**
     * 获取经纬度详细地址信息
     */
    async getlocation() {
        // 数据校验
        let rules = {
            latitude: 'must|string|name=纬度',
            longitude: 'must|string|name=经度',
        };
        // 取得数据
        let params = this.validateData(rules);
        // 内容审核
        await contentCheck.checkTextMultiClient(params);

        const res = { code: -1 };
        let { latitude, longitude } = params;
        let location = latitude + ',' + longitude;
        try {
            // 调用request请求封装，传入经纬度信息
            let url = `https://apis.map.qq.com/ws/geocoder/v1/?key=${LBS_KEY}` // API地址，加key
            let apiData = await request.get(url, { location: location });
            let result = apiData.result;
            // 如果返回有推荐地址，正常返回信息
            if (result.formatted_addresses != null) {
                res.code = 0
                res.formatted = result.formatted_addresses.recommend
                res.location = `${result.location.lat.toFixed(4)},${result.location.lng.toFixed(4)}`
                res.address = result.address
                res.adinfo = result.ad_info.name
                res.reallocal = result.location
                res.address_component = result.address_component
            } else {
                res.msg = '请确认经纬度在中国区域'
            }
        } catch (e) {
            res.msg = e
        }
        return res
    }

    /**
     * 搜索地点
     */
    async searchplace() {
        let keyword = this._event.params['keyword'] ?? '';
        let lat = this._event.params['lat'] ?? 23.107129;
        let lng = this._event.params['lng'] ?? 113.324778;
        let res = { code: -1 };
        try {
            // 调用request请求封装，传入经纬度信息
            let params = {
                keyword: encodeURI(keyword),
                boundary: `nearby(${lat},${lng},1000,1)`
            };
            let url = `https://apis.map.qq.com/ws/place/v1/search?key=${LBS_KEY}` // API地址，加key
            let result = await request.get(url, params);
            // 如果返回有推荐地址，正常返回信息
            if (result.data && result.data.length > 0) {
                res.code = 0;
                res.data = result.data[0];
            } else {
                res.msg = '请确认地址是否正确~'
            }
        } catch (e) {
            res.msg = e
        }
        return res
    }
}

module.exports = CommonController;